<?php
namespace app\daili\controller;
use think\facade\Db;
use think\facade\Request;

class User extends  Base
{

    //友情链接列表
    public function index()
    {
            $loginAdmin=session('dailiAccount'); 
        $uid=$loginAdmin['id'];
      $userData=Db::name('users')->where('uid',$uid)->paginate(15);
      return view('',[
         'userData'=>$userData,
         'left_menu'=>3
        ]);

    }
    public function edit()
    {
      $id=input('get.id');  
      $userData=Db::name('users')->where('id',$id)->find();
       if(request()->isPost()){
         $dataress = input('post.'); 
         $data['username'] =$dataress['username'];
         $data['busername'] =$dataress['busername'];
         $data['amount'] = $userData['amount']+$dataress['amount'];
         $data['email'] =$dataress['email'];
         if($data['password']=$userData['password']){
           $data['password']=$userData['password'];
         }else{
           $data['password']=$this->password_salt($dataress['password']);  
         }
         if($dataress['time'] !== NULL){
           if($userData['overtime'] !== NULL){
           $data['vip'] ='1';
           $data['viptime'] =$userData['viptime'];
           $data['overtime'] =$userData['overtime']+$dataress['time']*24*60*60;
         }else{
           $data['vip'] ='1';
           $data['viptime'] =time();
           $data['overtime'] =time+$dataress['time']*24*60*60;
         }  
         }
        else{
             $data['vip'] ='0'; 
         }
        
            $res=Db::name('users')->where('id',$id)->update($data);

            if($res){
				return alert('添加数据成功！跳转至参数设置','index',6,3);
			 }else{
				return alert('操作失败！','index',5,3);
			 }
       }
      return view('',[
         'userData'=>$userData,
         'left_menu'=>3
        ]);

    }
     public function add()
    {
       if(request()->isPost()){
         $dataress = input('post.'); 
         $data['username'] =$dataress['username'];
         $data['busername'] =$dataress['busername'];
         $data['amount'] =$dataress['amount'];
         $data['email'] =$dataress['email'];
         $data['status'] ='3';
         $data['password']=$this->password_salt($dataress['password']);
         if($dataress['time'] !== NULL ){
           $data['vip'] ='1';
           $data['viptime'] =time();
           $data['overtime'] =time()+$dataress['time']*24*60*60;
         }else{
             $data['vip'] ='0'; 
         }
        
         $res=Db::name('users')->insert($data);
         if($res){
            return alert('操作成功！','index',6,3);
         }else{
            return alert('操作失败！','index',5,3);
         }
       }
      
      return view('',[
      
        ]);

    }
    public function del()
    {
       $id=input('id');//助手函数

      $res=Db::name('users')->delete($id);
      if($res){
            return alert('删除成功!','index',6,3);
         }else{
            return alert('操作失败！','index',5,3);
         } 
    }
      public function order()
    {
          $loginAdmin=session('dailiAccount'); 
        $uid=$loginAdmin['id'];
      $userData=Db::name('order')->where('status',2)->where('dailiuid',$uid)->paginate(15);
      return view('',[
         'orderData'=>$userData,
      
        ]);

    }
   
     public function yongjin()
    {
        $loginAdmin=session('dailiAccount'); 
        $uid=$loginAdmin['id'];
        $userData=Db::name('daili')->where('id',$uid)->find();
         $tixian=Db::name('daili_tixian')->where('uid',$uid)->where('status',0)->sum('amount');
           if(request()->isPost()){
             $jine = input('jine');
             if($userData['hsoukuanren']==null){
                return alert('未设置收款方式，为您跳转至设置页面','ziliao',6,3);  
             }else{
               $data['amount']= $jine;
               $data['uid']= $uid;
               $res=Db::name('daili_tixian')->insert($data);
               $ress= Db::name('daili')->where('id',$uid)->dec('yue',$jine)->update();
                 $tongzhidata=Db::name('tongzhi')->where('id',1)->find();
                 if($tongzhidata['tixian']==2){
                    $post_data='
                    {
                    "msgtype": "markdown",
                    "markdown": {
                    "content": "代理提现通知\n
                     >所属代理:<font color=\"comment\">'.$userData['beizhu'].'</font>
                     >金额:<font color=\"comment\">'.$jine.'元</font>"
                    }
                     }';
                    $result = $this->tongzhi($post_data);  
   }
               
               
                if($res){
            return alert('操作成功！','yongjin',6,3);
         }else{
            return alert('操作失败！','yongjin',5,3);
         }
             }
           }
           
         return view('',[
         'userData'=>$userData,
       'tixian'=>$tixian,
        ]);

    }
      public function ziliao()
    {
      
        $loginAdmin=session('dailiAccount'); 
        $uid=$loginAdmin['id'];
        $userData=Db::name('daili')->where('id',$uid)->find();
           if(request()->isPost()){
             $data = input('post.');
             $res=Db::name('daili')->where('id',$uid)->update($data);
         if($res){
            return alert('操作成功！','ziliao',6,3);
         }else{
            return alert('操作失败！','ziliao',5,3);
         }
           }
         return view('',[
         'userData'=>$userData,
      
        ]);

    }
    /*添加*/
   
    
    
}
